using Dapper;
using Lzfy_His_Service.Models;

namespace Lzfy_His_Service.Services
{
    /// <summary>
    /// 退费任务操作日志服务实现
    /// </summary>
    public class RefundTaskOpLogService : IRefundTaskOpLogService
    {
        private readonly IOracleConnectionFactory _oracleConnectionFactory;
        private readonly ILogger<RefundTaskOpLogService> _logger;

        public RefundTaskOpLogService(IOracleConnectionFactory oracleConnectionFactory, ILogger<RefundTaskOpLogService> logger)
        {
            _oracleConnectionFactory = oracleConnectionFactory;
            _logger = logger;
        }

        /// <summary>
        /// 创建操作日志
        /// </summary>
        /// <param name="request">操作日志请求</param>
        /// <returns>是否成功</returns>
        public async Task<bool> CreateOpLogAsync(CreateOpLogRequest request)
        {
            try
            {
                using var connection = _oracleConnectionFactory.CreateConnection();
                
                const string sql = @"
                    INSERT INTO PCA.LZFY_REFUND_TASK_OP_LOGS 
                    (TASK_DETAIL_ID, OP_NAME, USERNAME, FULLNAME, ACTIONID) 
                    VALUES 
                    (:TaskDetailId, :OpName, :Username, :Fullname, :ActionId)";

                var parameters = new
                {
                    TaskDetailId = request.TaskDetailId,
                    OpName = request.OpName,
                    Username = request.Username,
                    Fullname = request.Fullname,
                    ActionId = request.ActionId
                };

                var result = await connection.ExecuteAsync(sql, parameters);
                
                _logger.LogInformation("操作日志创建成功，TaskDetailId: {TaskDetailId}, OpName: {OpName}, Username: {Username}", 
                    request.TaskDetailId, request.OpName, request.Username);
                
                return result > 0;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "创建操作日志失败，TaskDetailId: {TaskDetailId}, OpName: {OpName}, Username: {Username}", 
                    request.TaskDetailId, request.OpName, request.Username);
                return false;
            }
        }
    }
}